# %tensorflow_version 2.x
import tensorflow
tensorflow.__version__
'2.7.0'
import random
random.seed(0)
# Ignore the warnings
import warnings
warnings.filterwarnings("ignore")
import os
import numpy as np
import pandas as pd
import cv2
from glob import glob
import tensorflow as tf
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from tensorflow.keras.layers import Conv2D, Activation, BatchNormalization
from tensorflow.keras.layers import UpSampling2D, Input, Concatenate
from tensorflow.keras.models import Model
from tensorflow.keras.applications import MobileNetV2
from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau
from tensorflow.keras.metrics import Recall, Precision
from tensorflow.keras import backend as K
from PIL import Image
from numpy import asarray
from sklearn.datasets import load_files
# Loading the images file
data = np.load('Data/images.npy', allow_pickle=True)
data.shape
(409, 2)
The file contains 409 images and labels. Let's view few images and their labels.
data[100][0]
array([[[200, 185, 118],
[206, 191, 124],
[203, 188, 121],
...,
[204, 146, 46],
[212, 154, 54],
[205, 147, 47]],
[[203, 188, 121],
[206, 191, 124],
[200, 185, 118],
...,
[203, 145, 45],
[208, 150, 50],
[214, 156, 56]],
[[199, 184, 117],
[203, 188, 121],
[202, 187, 120],
...,
[203, 145, 45],
[193, 135, 35],
[202, 144, 44]],
...,
[[ 63, 1, 2],
[ 66, 2, 3],
[ 66, 2, 2],
...,
[ 3, 0, 0],
[ 4, 0, 0],
[ 4, 0, 0]],
[[ 66, 2, 3],
[ 66, 2, 3],
[ 67, 3, 3],
...,
[ 1, 0, 0],
[ 2, 1, 0],
[ 3, 2, 0]],
[[ 64, 0, 1],
[ 64, 0, 0],
[ 66, 0, 1],
...,
[ 1, 1, 0],
[ 2, 2, 0],
[ 3, 3, 1]]], dtype=uint8)
data[23][1]
[{'label': ['Face'],
'notes': '',
'points': [{'x': 0.22545454545454546, 'y': 0.37142857142857144},
{'x': 0.49454545454545457, 'y': 0.7971428571428572}],
'imageWidth': 275,
'imageHeight': 350},
{'label': ['Face'],
'notes': '',
'points': [{'x': 0.4218181818181818, 'y': 0.5228571428571429},
{'x': 0.7527272727272727, 'y': 0.8628571428571429}],
'imageWidth': 275,
'imageHeight': 350}]
cv2.imshow('img',data[23][0])
# waits for user to press any key
# (this is necessary to avoid Python kernel form crashing)
cv2.waitKey(0)
# closing all open windows
cv2.destroyAllWindows()
fi,ax = plt.subplots(10,3,figsize=(20,30))
row = 0
col = 0
index = 0
for i in range(30):
ax[row][col].imshow(data[index][0], interpolation='nearest')
index = index + 6
col = col + 1
if col > 2:
row = row + 1
col = 0
plt.show()
Image Pre-Processing
from tensorflow.keras.applications.mobilenet import preprocess_input
IMAGE_HEIGHT = 224
IMAGE_WIDTH = 224
HEIGHT_CELLS = 28
WIDTH_CELLS = 28
IMAGE_SIZE = 224
masks = np.zeros((int(data.shape[0]), IMAGE_HEIGHT, IMAGE_WIDTH))
X = np.zeros((int(data.shape[0]),IMAGE_HEIGHT, IMAGE_WIDTH, 3))
for index in range(data.shape[0]):
img = data[index][0]
img = cv2.resize(img, dsize=(IMAGE_HEIGHT, IMAGE_WIDTH), interpolation=cv2.INTER_CUBIC)
# assign all pixels in the first 3 channels only to the image, i.e., discard the alpha channel
try:
img = img[:,:,:3]
except:
print(f"Exception {index} Grayscale image with shape {img.shape}")
# convert the grayscale image to color so that the number of channels are standardized to 3
img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)
continue
X[index] = preprocess_input(np.array(img, dtype=np.float32))
# Loop through the face co-ordinates and create mask out of it.
for i in data[index][1]:
x1 = int(i['points'][0]['x'] * IMAGE_WIDTH)
x2 = int(i['points'][1]['x'] * IMAGE_WIDTH)
y1 = int(i['points'][0]['y'] * IMAGE_HEIGHT)
y2 = int(i['points'][1]['y'] * IMAGE_HEIGHT)
# set all pixels within the mask co-ordinates to 1.
masks[index][y1:y2, x1:x2] = 1
print(f"### Shape of X is '{X.shape}' and the shape of mask is '{masks.shape}' ")
Exception 272 Grayscale image with shape (224, 224) ### Shape of X is '(409, 224, 224, 3)' and the shape of mask is '(409, 224, 224)'
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, masks, test_size=0.2)
X_val, X_test, y_val, y_test = train_test_split(X_test, y_test, test_size=0.2)
print(f"Shape of X_train is '{X_train.shape}' and the shape of y_train is '{y_train.shape}'")
print(f"Shape of X_val is '{X_val.shape}' and the shape of y_val is '{y_val.shape}'")
print(f"Shape of X_test is '{X_test.shape}' and the shape of y_test is '{y_test.shape}'")
Shape of X_train is '(327, 224, 224, 3)' and the shape of y_train is '(327, 224, 224)' Shape of X_val is '(65, 224, 224, 3)' and the shape of y_val is '(65, 224, 224)' Shape of X_test is '(17, 224, 224, 3)' and the shape of y_test is '(17, 224, 224)'
fig = plt.figure(figsize=(15, 15))
a = fig.add_subplot(1, 4, 1)
imgplot = plt.imshow(X_train[0])
a = fig.add_subplot(1, 4, 2)
imgplot = plt.imshow(X_train[10])
imgplot.set_clim(0.0, 0.7)
a = fig.add_subplot(1, 4, 3)
imgplot = plt.imshow(X_train[20])
imgplot.set_clim(0.0, 1.4)
a = fig.add_subplot(1, 4, 4)
imgplot = plt.imshow(X_train[30])
imgplot.set_clim(0.0, 2.1)
fig = plt.figure(figsize=(15, 15))
a = fig.add_subplot(1, 4, 1)
imgplot = plt.imshow(y_train[0])
a = fig.add_subplot(1, 4, 2)
imgplot = plt.imshow(y_train[10])
imgplot.set_clim(0.0, 0.7)
a = fig.add_subplot(1, 4, 3)
imgplot = plt.imshow(y_train[20])
imgplot.set_clim(0.0, 1.4)
a = fig.add_subplot(1, 4, 4)
imgplot = plt.imshow(y_train[30])
imgplot.set_clim(0.0, 1.4)
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Creating a Mask Detection Model using U-net with MobileNet Transfer Learning Model
IMAGE_SIZE = 224
EPOCHS = 30
BATCH = 8
LR = 1e-4
def model():
inputs = Input(shape=(IMAGE_SIZE, IMAGE_SIZE, 3), name="input_image")
encoder = MobileNetV2(input_tensor=inputs, weights="imagenet", include_top=False, alpha=0.35)
skip_connection_names = ["input_image", "block_1_expand_relu", "block_3_expand_relu", "block_6_expand_relu"]
encoder_output = encoder.get_layer("block_13_expand_relu").output
f = [16, 32, 48, 64]
x = encoder_output
for i in range(1, len(skip_connection_names)+1, 1):
x_skip = encoder.get_layer(skip_connection_names[-i]).output
x = UpSampling2D((2, 2))(x)
x = Concatenate()([x, x_skip])
x = Conv2D(f[-i], (3, 3), padding="same")(x)
x = BatchNormalization()(x)
x = Activation("relu")(x)
x = Conv2D(f[-i], (3, 3), padding="same")(x)
x = BatchNormalization()(x)
x = Activation("relu")(x)
x = Conv2D(1, (1, 1), padding="same")(x)
x = Activation("sigmoid")(x)
model = Model(inputs, x)
return model
model = model()
model.summary()
WARNING:tensorflow:`input_shape` is undefined or non-square, or `rows` is not in [96, 128, 160, 192, 224]. Weights for input shape (224, 224) will be loaded as the default.
Model: "model_3"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_image (InputLayer) [(None, 224, 224, 3 0 []
)]
Conv1 (Conv2D) (None, 112, 112, 16 432 ['input_image[0][0]']
)
bn_Conv1 (BatchNormalization) (None, 112, 112, 16 64 ['Conv1[0][0]']
)
Conv1_relu (ReLU) (None, 112, 112, 16 0 ['bn_Conv1[0][0]']
)
expanded_conv_depthwise (Depth (None, 112, 112, 16 144 ['Conv1_relu[0][0]']
wiseConv2D) )
expanded_conv_depthwise_BN (Ba (None, 112, 112, 16 64 ['expanded_conv_depthwise[0][0]']
tchNormalization) )
expanded_conv_depthwise_relu ( (None, 112, 112, 16 0 ['expanded_conv_depthwise_BN[0][0
ReLU) ) ]']
expanded_conv_project (Conv2D) (None, 112, 112, 8) 128 ['expanded_conv_depthwise_relu[0]
[0]']
expanded_conv_project_BN (Batc (None, 112, 112, 8) 32 ['expanded_conv_project[0][0]']
hNormalization)
block_1_expand (Conv2D) (None, 112, 112, 48 384 ['expanded_conv_project_BN[0][0]'
) ]
block_1_expand_BN (BatchNormal (None, 112, 112, 48 192 ['block_1_expand[0][0]']
ization) )
block_1_expand_relu (ReLU) (None, 112, 112, 48 0 ['block_1_expand_BN[0][0]']
)
block_1_pad (ZeroPadding2D) (None, 113, 113, 48 0 ['block_1_expand_relu[0][0]']
)
block_1_depthwise (DepthwiseCo (None, 56, 56, 48) 432 ['block_1_pad[0][0]']
nv2D)
block_1_depthwise_BN (BatchNor (None, 56, 56, 48) 192 ['block_1_depthwise[0][0]']
malization)
block_1_depthwise_relu (ReLU) (None, 56, 56, 48) 0 ['block_1_depthwise_BN[0][0]']
block_1_project (Conv2D) (None, 56, 56, 8) 384 ['block_1_depthwise_relu[0][0]']
block_1_project_BN (BatchNorma (None, 56, 56, 8) 32 ['block_1_project[0][0]']
lization)
block_2_expand (Conv2D) (None, 56, 56, 48) 384 ['block_1_project_BN[0][0]']
block_2_expand_BN (BatchNormal (None, 56, 56, 48) 192 ['block_2_expand[0][0]']
ization)
block_2_expand_relu (ReLU) (None, 56, 56, 48) 0 ['block_2_expand_BN[0][0]']
block_2_depthwise (DepthwiseCo (None, 56, 56, 48) 432 ['block_2_expand_relu[0][0]']
nv2D)
block_2_depthwise_BN (BatchNor (None, 56, 56, 48) 192 ['block_2_depthwise[0][0]']
malization)
block_2_depthwise_relu (ReLU) (None, 56, 56, 48) 0 ['block_2_depthwise_BN[0][0]']
block_2_project (Conv2D) (None, 56, 56, 8) 384 ['block_2_depthwise_relu[0][0]']
block_2_project_BN (BatchNorma (None, 56, 56, 8) 32 ['block_2_project[0][0]']
lization)
block_2_add (Add) (None, 56, 56, 8) 0 ['block_1_project_BN[0][0]',
'block_2_project_BN[0][0]']
block_3_expand (Conv2D) (None, 56, 56, 48) 384 ['block_2_add[0][0]']
block_3_expand_BN (BatchNormal (None, 56, 56, 48) 192 ['block_3_expand[0][0]']
ization)
block_3_expand_relu (ReLU) (None, 56, 56, 48) 0 ['block_3_expand_BN[0][0]']
block_3_pad (ZeroPadding2D) (None, 57, 57, 48) 0 ['block_3_expand_relu[0][0]']
block_3_depthwise (DepthwiseCo (None, 28, 28, 48) 432 ['block_3_pad[0][0]']
nv2D)
block_3_depthwise_BN (BatchNor (None, 28, 28, 48) 192 ['block_3_depthwise[0][0]']
malization)
block_3_depthwise_relu (ReLU) (None, 28, 28, 48) 0 ['block_3_depthwise_BN[0][0]']
block_3_project (Conv2D) (None, 28, 28, 16) 768 ['block_3_depthwise_relu[0][0]']
block_3_project_BN (BatchNorma (None, 28, 28, 16) 64 ['block_3_project[0][0]']
lization)
block_4_expand (Conv2D) (None, 28, 28, 96) 1536 ['block_3_project_BN[0][0]']
block_4_expand_BN (BatchNormal (None, 28, 28, 96) 384 ['block_4_expand[0][0]']
ization)
block_4_expand_relu (ReLU) (None, 28, 28, 96) 0 ['block_4_expand_BN[0][0]']
block_4_depthwise (DepthwiseCo (None, 28, 28, 96) 864 ['block_4_expand_relu[0][0]']
nv2D)
block_4_depthwise_BN (BatchNor (None, 28, 28, 96) 384 ['block_4_depthwise[0][0]']
malization)
block_4_depthwise_relu (ReLU) (None, 28, 28, 96) 0 ['block_4_depthwise_BN[0][0]']
block_4_project (Conv2D) (None, 28, 28, 16) 1536 ['block_4_depthwise_relu[0][0]']
block_4_project_BN (BatchNorma (None, 28, 28, 16) 64 ['block_4_project[0][0]']
lization)
block_4_add (Add) (None, 28, 28, 16) 0 ['block_3_project_BN[0][0]',
'block_4_project_BN[0][0]']
block_5_expand (Conv2D) (None, 28, 28, 96) 1536 ['block_4_add[0][0]']
block_5_expand_BN (BatchNormal (None, 28, 28, 96) 384 ['block_5_expand[0][0]']
ization)
block_5_expand_relu (ReLU) (None, 28, 28, 96) 0 ['block_5_expand_BN[0][0]']
block_5_depthwise (DepthwiseCo (None, 28, 28, 96) 864 ['block_5_expand_relu[0][0]']
nv2D)
block_5_depthwise_BN (BatchNor (None, 28, 28, 96) 384 ['block_5_depthwise[0][0]']
malization)
block_5_depthwise_relu (ReLU) (None, 28, 28, 96) 0 ['block_5_depthwise_BN[0][0]']
block_5_project (Conv2D) (None, 28, 28, 16) 1536 ['block_5_depthwise_relu[0][0]']
block_5_project_BN (BatchNorma (None, 28, 28, 16) 64 ['block_5_project[0][0]']
lization)
block_5_add (Add) (None, 28, 28, 16) 0 ['block_4_add[0][0]',
'block_5_project_BN[0][0]']
block_6_expand (Conv2D) (None, 28, 28, 96) 1536 ['block_5_add[0][0]']
block_6_expand_BN (BatchNormal (None, 28, 28, 96) 384 ['block_6_expand[0][0]']
ization)
block_6_expand_relu (ReLU) (None, 28, 28, 96) 0 ['block_6_expand_BN[0][0]']
block_6_pad (ZeroPadding2D) (None, 29, 29, 96) 0 ['block_6_expand_relu[0][0]']
block_6_depthwise (DepthwiseCo (None, 14, 14, 96) 864 ['block_6_pad[0][0]']
nv2D)
block_6_depthwise_BN (BatchNor (None, 14, 14, 96) 384 ['block_6_depthwise[0][0]']
malization)
block_6_depthwise_relu (ReLU) (None, 14, 14, 96) 0 ['block_6_depthwise_BN[0][0]']
block_6_project (Conv2D) (None, 14, 14, 24) 2304 ['block_6_depthwise_relu[0][0]']
block_6_project_BN (BatchNorma (None, 14, 14, 24) 96 ['block_6_project[0][0]']
lization)
block_7_expand (Conv2D) (None, 14, 14, 144) 3456 ['block_6_project_BN[0][0]']
block_7_expand_BN (BatchNormal (None, 14, 14, 144) 576 ['block_7_expand[0][0]']
ization)
block_7_expand_relu (ReLU) (None, 14, 14, 144) 0 ['block_7_expand_BN[0][0]']
block_7_depthwise (DepthwiseCo (None, 14, 14, 144) 1296 ['block_7_expand_relu[0][0]']
nv2D)
block_7_depthwise_BN (BatchNor (None, 14, 14, 144) 576 ['block_7_depthwise[0][0]']
malization)
block_7_depthwise_relu (ReLU) (None, 14, 14, 144) 0 ['block_7_depthwise_BN[0][0]']
block_7_project (Conv2D) (None, 14, 14, 24) 3456 ['block_7_depthwise_relu[0][0]']
block_7_project_BN (BatchNorma (None, 14, 14, 24) 96 ['block_7_project[0][0]']
lization)
block_7_add (Add) (None, 14, 14, 24) 0 ['block_6_project_BN[0][0]',
'block_7_project_BN[0][0]']
block_8_expand (Conv2D) (None, 14, 14, 144) 3456 ['block_7_add[0][0]']
block_8_expand_BN (BatchNormal (None, 14, 14, 144) 576 ['block_8_expand[0][0]']
ization)
block_8_expand_relu (ReLU) (None, 14, 14, 144) 0 ['block_8_expand_BN[0][0]']
block_8_depthwise (DepthwiseCo (None, 14, 14, 144) 1296 ['block_8_expand_relu[0][0]']
nv2D)
block_8_depthwise_BN (BatchNor (None, 14, 14, 144) 576 ['block_8_depthwise[0][0]']
malization)
block_8_depthwise_relu (ReLU) (None, 14, 14, 144) 0 ['block_8_depthwise_BN[0][0]']
block_8_project (Conv2D) (None, 14, 14, 24) 3456 ['block_8_depthwise_relu[0][0]']
block_8_project_BN (BatchNorma (None, 14, 14, 24) 96 ['block_8_project[0][0]']
lization)
block_8_add (Add) (None, 14, 14, 24) 0 ['block_7_add[0][0]',
'block_8_project_BN[0][0]']
block_9_expand (Conv2D) (None, 14, 14, 144) 3456 ['block_8_add[0][0]']
block_9_expand_BN (BatchNormal (None, 14, 14, 144) 576 ['block_9_expand[0][0]']
ization)
block_9_expand_relu (ReLU) (None, 14, 14, 144) 0 ['block_9_expand_BN[0][0]']
block_9_depthwise (DepthwiseCo (None, 14, 14, 144) 1296 ['block_9_expand_relu[0][0]']
nv2D)
block_9_depthwise_BN (BatchNor (None, 14, 14, 144) 576 ['block_9_depthwise[0][0]']
malization)
block_9_depthwise_relu (ReLU) (None, 14, 14, 144) 0 ['block_9_depthwise_BN[0][0]']
block_9_project (Conv2D) (None, 14, 14, 24) 3456 ['block_9_depthwise_relu[0][0]']
block_9_project_BN (BatchNorma (None, 14, 14, 24) 96 ['block_9_project[0][0]']
lization)
block_9_add (Add) (None, 14, 14, 24) 0 ['block_8_add[0][0]',
'block_9_project_BN[0][0]']
block_10_expand (Conv2D) (None, 14, 14, 144) 3456 ['block_9_add[0][0]']
block_10_expand_BN (BatchNorma (None, 14, 14, 144) 576 ['block_10_expand[0][0]']
lization)
block_10_expand_relu (ReLU) (None, 14, 14, 144) 0 ['block_10_expand_BN[0][0]']
block_10_depthwise (DepthwiseC (None, 14, 14, 144) 1296 ['block_10_expand_relu[0][0]']
onv2D)
block_10_depthwise_BN (BatchNo (None, 14, 14, 144) 576 ['block_10_depthwise[0][0]']
rmalization)
block_10_depthwise_relu (ReLU) (None, 14, 14, 144) 0 ['block_10_depthwise_BN[0][0]']
block_10_project (Conv2D) (None, 14, 14, 32) 4608 ['block_10_depthwise_relu[0][0]']
block_10_project_BN (BatchNorm (None, 14, 14, 32) 128 ['block_10_project[0][0]']
alization)
block_11_expand (Conv2D) (None, 14, 14, 192) 6144 ['block_10_project_BN[0][0]']
block_11_expand_BN (BatchNorma (None, 14, 14, 192) 768 ['block_11_expand[0][0]']
lization)
block_11_expand_relu (ReLU) (None, 14, 14, 192) 0 ['block_11_expand_BN[0][0]']
block_11_depthwise (DepthwiseC (None, 14, 14, 192) 1728 ['block_11_expand_relu[0][0]']
onv2D)
block_11_depthwise_BN (BatchNo (None, 14, 14, 192) 768 ['block_11_depthwise[0][0]']
rmalization)
block_11_depthwise_relu (ReLU) (None, 14, 14, 192) 0 ['block_11_depthwise_BN[0][0]']
block_11_project (Conv2D) (None, 14, 14, 32) 6144 ['block_11_depthwise_relu[0][0]']
block_11_project_BN (BatchNorm (None, 14, 14, 32) 128 ['block_11_project[0][0]']
alization)
block_11_add (Add) (None, 14, 14, 32) 0 ['block_10_project_BN[0][0]',
'block_11_project_BN[0][0]']
block_12_expand (Conv2D) (None, 14, 14, 192) 6144 ['block_11_add[0][0]']
block_12_expand_BN (BatchNorma (None, 14, 14, 192) 768 ['block_12_expand[0][0]']
lization)
block_12_expand_relu (ReLU) (None, 14, 14, 192) 0 ['block_12_expand_BN[0][0]']
block_12_depthwise (DepthwiseC (None, 14, 14, 192) 1728 ['block_12_expand_relu[0][0]']
onv2D)
block_12_depthwise_BN (BatchNo (None, 14, 14, 192) 768 ['block_12_depthwise[0][0]']
rmalization)
block_12_depthwise_relu (ReLU) (None, 14, 14, 192) 0 ['block_12_depthwise_BN[0][0]']
block_12_project (Conv2D) (None, 14, 14, 32) 6144 ['block_12_depthwise_relu[0][0]']
block_12_project_BN (BatchNorm (None, 14, 14, 32) 128 ['block_12_project[0][0]']
alization)
block_12_add (Add) (None, 14, 14, 32) 0 ['block_11_add[0][0]',
'block_12_project_BN[0][0]']
block_13_expand (Conv2D) (None, 14, 14, 192) 6144 ['block_12_add[0][0]']
block_13_expand_BN (BatchNorma (None, 14, 14, 192) 768 ['block_13_expand[0][0]']
lization)
block_13_expand_relu (ReLU) (None, 14, 14, 192) 0 ['block_13_expand_BN[0][0]']
up_sampling2d (UpSampling2D) (None, 28, 28, 192) 0 ['block_13_expand_relu[0][0]']
concatenate (Concatenate) (None, 28, 28, 288) 0 ['up_sampling2d[0][0]',
'block_6_expand_relu[0][0]']
conv2d_48 (Conv2D) (None, 28, 28, 64) 165952 ['concatenate[0][0]']
batch_normalization (BatchNorm (None, 28, 28, 64) 256 ['conv2d_48[0][0]']
alization)
activation_3 (Activation) (None, 28, 28, 64) 0 ['batch_normalization[0][0]']
conv2d_49 (Conv2D) (None, 28, 28, 64) 36928 ['activation_3[0][0]']
batch_normalization_1 (BatchNo (None, 28, 28, 64) 256 ['conv2d_49[0][0]']
rmalization)
activation_4 (Activation) (None, 28, 28, 64) 0 ['batch_normalization_1[0][0]']
up_sampling2d_1 (UpSampling2D) (None, 56, 56, 64) 0 ['activation_4[0][0]']
concatenate_1 (Concatenate) (None, 56, 56, 112) 0 ['up_sampling2d_1[0][0]',
'block_3_expand_relu[0][0]']
conv2d_50 (Conv2D) (None, 56, 56, 48) 48432 ['concatenate_1[0][0]']
batch_normalization_2 (BatchNo (None, 56, 56, 48) 192 ['conv2d_50[0][0]']
rmalization)
activation_5 (Activation) (None, 56, 56, 48) 0 ['batch_normalization_2[0][0]']
conv2d_51 (Conv2D) (None, 56, 56, 48) 20784 ['activation_5[0][0]']
batch_normalization_3 (BatchNo (None, 56, 56, 48) 192 ['conv2d_51[0][0]']
rmalization)
activation_6 (Activation) (None, 56, 56, 48) 0 ['batch_normalization_3[0][0]']
up_sampling2d_2 (UpSampling2D) (None, 112, 112, 48 0 ['activation_6[0][0]']
)
concatenate_2 (Concatenate) (None, 112, 112, 96 0 ['up_sampling2d_2[0][0]',
) 'block_1_expand_relu[0][0]']
conv2d_52 (Conv2D) (None, 112, 112, 32 27680 ['concatenate_2[0][0]']
)
batch_normalization_4 (BatchNo (None, 112, 112, 32 128 ['conv2d_52[0][0]']
rmalization) )
activation_7 (Activation) (None, 112, 112, 32 0 ['batch_normalization_4[0][0]']
)
conv2d_53 (Conv2D) (None, 112, 112, 32 9248 ['activation_7[0][0]']
)
batch_normalization_5 (BatchNo (None, 112, 112, 32 128 ['conv2d_53[0][0]']
rmalization) )
activation_8 (Activation) (None, 112, 112, 32 0 ['batch_normalization_5[0][0]']
)
up_sampling2d_3 (UpSampling2D) (None, 224, 224, 32 0 ['activation_8[0][0]']
)
concatenate_3 (Concatenate) (None, 224, 224, 35 0 ['up_sampling2d_3[0][0]',
) 'input_image[0][0]']
conv2d_54 (Conv2D) (None, 224, 224, 16 5056 ['concatenate_3[0][0]']
)
batch_normalization_6 (BatchNo (None, 224, 224, 16 64 ['conv2d_54[0][0]']
rmalization) )
activation_9 (Activation) (None, 224, 224, 16 0 ['batch_normalization_6[0][0]']
)
conv2d_55 (Conv2D) (None, 224, 224, 16 2320 ['activation_9[0][0]']
)
batch_normalization_7 (BatchNo (None, 224, 224, 16 64 ['conv2d_55[0][0]']
rmalization) )
activation_10 (Activation) (None, 224, 224, 16 0 ['batch_normalization_7[0][0]']
)
conv2d_56 (Conv2D) (None, 224, 224, 1) 17 ['activation_10[0][0]']
activation_11 (Activation) (None, 224, 224, 1) 0 ['conv2d_56[0][0]']
==================================================================================================
Total params: 416,209
Trainable params: 409,025
Non-trainable params: 7,184
__________________________________________________________________________________________________
Designing Dice Coefficient and Loss function
smooth = 1e-15
def dice_coef(y_true, y_pred):
y_true = tf.keras.layers.Flatten()(y_true)
y_pred = tf.keras.layers.Flatten()(y_pred)
intersection = tf.reduce_sum(y_true * y_pred)
return (2. * intersection + smooth) / (tf.reduce_sum(y_true) + tf.reduce_sum(y_pred) + smooth)
def dice_loss(y_true, y_pred):
return 1.0 - dice_coef(y_true, y_pred)
Compliling the Model
opt = tf.keras.optimizers.Nadam(LR)
metrics = [dice_coef, Recall(), Precision()]
model.compile(loss=dice_loss, optimizer=opt, metrics=metrics)
callbacks = [
ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=4),
EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True)
]
train_steps = len(X_train)//BATCH
valid_steps = len(X_val)//BATCH
if len(X_train) % BATCH != 0:
train_steps += 1
if len(X_val) % BATCH != 0:
valid_steps += 1
model.fit(
X_train, y_train,
validation_data=(X_val, y_val),
epochs=EPOCHS,
steps_per_epoch=train_steps,
validation_steps=valid_steps,
callbacks=callbacks
)
Epoch 1/30 41/41 [==============================] - 77s 2s/step - loss: 0.7340 - dice_coef: 0.2659 - recall: 0.8873 - precision: 0.1824 - val_loss: 0.7599 - val_dice_coef: 0.2248 - val_recall: 1.0000 - val_precision: 0.1307 - lr: 1.0000e-04 Epoch 2/30 41/41 [==============================] - 63s 2s/step - loss: 0.6419 - dice_coef: 0.3583 - recall: 0.9332 - precision: 0.3236 - val_loss: 0.7260 - val_dice_coef: 0.2560 - val_recall: 0.9995 - val_precision: 0.1492 - lr: 1.0000e-04 Epoch 3/30 41/41 [==============================] - 67s 2s/step - loss: 0.6114 - dice_coef: 0.3887 - recall: 0.9459 - precision: 0.3712 - val_loss: 0.6714 - val_dice_coef: 0.3075 - val_recall: 0.9921 - val_precision: 0.2372 - lr: 1.0000e-04 Epoch 4/30 41/41 [==============================] - 75s 2s/step - loss: 0.5916 - dice_coef: 0.4084 - recall: 0.9593 - precision: 0.3807 - val_loss: 0.6448 - val_dice_coef: 0.3342 - val_recall: 0.9870 - val_precision: 0.2790 - lr: 1.0000e-04 Epoch 5/30 41/41 [==============================] - 78s 2s/step - loss: 0.5831 - dice_coef: 0.4169 - recall: 0.9471 - precision: 0.4184 - val_loss: 0.6081 - val_dice_coef: 0.3701 - val_recall: 0.9732 - val_precision: 0.3508 - lr: 1.0000e-04 Epoch 6/30 41/41 [==============================] - 68s 2s/step - loss: 0.5636 - dice_coef: 0.4368 - recall: 0.9471 - precision: 0.4793 - val_loss: 0.5958 - val_dice_coef: 0.3828 - val_recall: 0.9556 - val_precision: 0.3888 - lr: 1.0000e-04 Epoch 7/30 41/41 [==============================] - 71s 2s/step - loss: 0.5579 - dice_coef: 0.4419 - recall: 0.9337 - precision: 0.5092 - val_loss: 0.5736 - val_dice_coef: 0.4051 - val_recall: 0.9196 - val_precision: 0.4657 - lr: 1.0000e-04 Epoch 8/30 41/41 [==============================] - 67s 2s/step - loss: 0.5488 - dice_coef: 0.4509 - recall: 0.9335 - precision: 0.5433 - val_loss: 0.5691 - val_dice_coef: 0.4091 - val_recall: 0.9187 - val_precision: 0.4784 - lr: 1.0000e-04 Epoch 9/30 41/41 [==============================] - 69s 2s/step - loss: 0.5389 - dice_coef: 0.4609 - recall: 0.9436 - precision: 0.5621 - val_loss: 0.5644 - val_dice_coef: 0.4145 - val_recall: 0.8933 - val_precision: 0.5101 - lr: 1.0000e-04 Epoch 10/30 41/41 [==============================] - 69s 2s/step - loss: 0.5371 - dice_coef: 0.4631 - recall: 0.9330 - precision: 0.5735 - val_loss: 0.5537 - val_dice_coef: 0.4254 - val_recall: 0.8829 - val_precision: 0.5371 - lr: 1.0000e-04 Epoch 11/30 41/41 [==============================] - 69s 2s/step - loss: 0.5264 - dice_coef: 0.4740 - recall: 0.9419 - precision: 0.5941 - val_loss: 0.5468 - val_dice_coef: 0.4321 - val_recall: 0.8258 - val_precision: 0.5975 - lr: 1.0000e-04 Epoch 12/30 41/41 [==============================] - 71s 2s/step - loss: 0.5215 - dice_coef: 0.4782 - recall: 0.9374 - precision: 0.6048 - val_loss: 0.5514 - val_dice_coef: 0.4279 - val_recall: 0.8536 - val_precision: 0.5539 - lr: 1.0000e-04 Epoch 13/30 41/41 [==============================] - 72s 2s/step - loss: 0.5139 - dice_coef: 0.4864 - recall: 0.9422 - precision: 0.6206 - val_loss: 0.5407 - val_dice_coef: 0.4396 - val_recall: 0.7772 - val_precision: 0.6243 - lr: 1.0000e-04 Epoch 14/30 41/41 [==============================] - 68s 2s/step - loss: 0.5105 - dice_coef: 0.4899 - recall: 0.9401 - precision: 0.6310 - val_loss: 0.5375 - val_dice_coef: 0.4416 - val_recall: 0.8481 - val_precision: 0.5516 - lr: 1.0000e-04 Epoch 15/30 41/41 [==============================] - 67s 2s/step - loss: 0.4967 - dice_coef: 0.5035 - recall: 0.9414 - precision: 0.6505 - val_loss: 0.5366 - val_dice_coef: 0.4438 - val_recall: 0.7474 - val_precision: 0.6301 - lr: 1.0000e-04 Epoch 16/30 41/41 [==============================] - 68s 2s/step - loss: 0.4915 - dice_coef: 0.5088 - recall: 0.9467 - precision: 0.6583 - val_loss: 0.5352 - val_dice_coef: 0.4453 - val_recall: 0.8091 - val_precision: 0.5732 - lr: 1.0000e-04 Epoch 17/30 41/41 [==============================] - 69s 2s/step - loss: 0.4854 - dice_coef: 0.5148 - recall: 0.9453 - precision: 0.6698 - val_loss: 0.5277 - val_dice_coef: 0.4528 - val_recall: 0.7919 - val_precision: 0.6003 - lr: 1.0000e-04 Epoch 18/30 41/41 [==============================] - 67s 2s/step - loss: 0.4752 - dice_coef: 0.5251 - recall: 0.9501 - precision: 0.6863 - val_loss: 0.5235 - val_dice_coef: 0.4550 - val_recall: 0.8142 - val_precision: 0.5736 - lr: 1.0000e-04 Epoch 19/30 41/41 [==============================] - 67s 2s/step - loss: 0.4764 - dice_coef: 0.5233 - recall: 0.9453 - precision: 0.6759 - val_loss: 0.5175 - val_dice_coef: 0.4620 - val_recall: 0.7632 - val_precision: 0.6241 - lr: 1.0000e-04 Epoch 20/30 41/41 [==============================] - 68s 2s/step - loss: 0.4680 - dice_coef: 0.5316 - recall: 0.9499 - precision: 0.6924 - val_loss: 0.5285 - val_dice_coef: 0.4552 - val_recall: 0.6273 - val_precision: 0.7005 - lr: 1.0000e-04 Epoch 21/30 41/41 [==============================] - 67s 2s/step - loss: 0.4598 - dice_coef: 0.5406 - recall: 0.9484 - precision: 0.7115 - val_loss: 0.5201 - val_dice_coef: 0.4633 - val_recall: 0.6495 - val_precision: 0.6950 - lr: 1.0000e-04 Epoch 22/30 41/41 [==============================] - 69s 2s/step - loss: 0.4508 - dice_coef: 0.5494 - recall: 0.9529 - precision: 0.7204 - val_loss: 0.5194 - val_dice_coef: 0.4635 - val_recall: 0.6356 - val_precision: 0.7002 - lr: 1.0000e-04 Epoch 23/30 41/41 [==============================] - 69s 2s/step - loss: 0.4479 - dice_coef: 0.5516 - recall: 0.9476 - precision: 0.7221 - val_loss: 0.5046 - val_dice_coef: 0.4766 - val_recall: 0.6917 - val_precision: 0.6640 - lr: 1.0000e-04 Epoch 24/30 41/41 [==============================] - 70s 2s/step - loss: 0.4412 - dice_coef: 0.5590 - recall: 0.9488 - precision: 0.7277 - val_loss: 0.5054 - val_dice_coef: 0.4753 - val_recall: 0.7433 - val_precision: 0.6160 - lr: 1.0000e-04 Epoch 25/30 41/41 [==============================] - 95s 2s/step - loss: 0.4315 - dice_coef: 0.5688 - recall: 0.9537 - precision: 0.7403 - val_loss: 0.5009 - val_dice_coef: 0.4803 - val_recall: 0.7028 - val_precision: 0.6677 - lr: 1.0000e-04 Epoch 26/30 41/41 [==============================] - 101s 2s/step - loss: 0.4230 - dice_coef: 0.5769 - recall: 0.9518 - precision: 0.7513 - val_loss: 0.5003 - val_dice_coef: 0.4810 - val_recall: 0.7093 - val_precision: 0.6555 - lr: 1.0000e-04 Epoch 27/30 41/41 [==============================] - 102s 3s/step - loss: 0.4122 - dice_coef: 0.5881 - recall: 0.9544 - precision: 0.7685 - val_loss: 0.5050 - val_dice_coef: 0.4791 - val_recall: 0.6331 - val_precision: 0.7074 - lr: 1.0000e-04 Epoch 28/30 41/41 [==============================] - 101s 2s/step - loss: 0.4056 - dice_coef: 0.5943 - recall: 0.9592 - precision: 0.7648 - val_loss: 0.5076 - val_dice_coef: 0.4763 - val_recall: 0.6261 - val_precision: 0.6961 - lr: 1.0000e-04 Epoch 29/30 41/41 [==============================] - 101s 2s/step - loss: 0.4003 - dice_coef: 0.6000 - recall: 0.9462 - precision: 0.7837 - val_loss: 0.4877 - val_dice_coef: 0.4927 - val_recall: 0.7427 - val_precision: 0.6302 - lr: 1.0000e-04 Epoch 30/30 41/41 [==============================] - 101s 2s/step - loss: 0.3968 - dice_coef: 0.6028 - recall: 0.9521 - precision: 0.7741 - val_loss: 0.5019 - val_dice_coef: 0.4839 - val_recall: 0.5885 - val_precision: 0.7437 - lr: 1.0000e-04
<keras.callbacks.History at 0x209b90606a0>
model.save('CVUnet.h5')
#load the model
new_model= tensorflow.keras.models.load_model('CVUnet.h5',custom_objects={'dice_loss': dice_loss,'dice_coef': dice_coef})
new_model.summary()
Model: "model"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_image (InputLayer) [(None, 224, 224, 3 0 []
)]
Conv1 (Conv2D) (None, 112, 112, 16 432 ['input_image[0][0]']
)
bn_Conv1 (BatchNormalization) (None, 112, 112, 16 64 ['Conv1[0][0]']
)
Conv1_relu (ReLU) (None, 112, 112, 16 0 ['bn_Conv1[0][0]']
)
expanded_conv_depthwise (Depth (None, 112, 112, 16 144 ['Conv1_relu[0][0]']
wiseConv2D) )
expanded_conv_depthwise_BN (Ba (None, 112, 112, 16 64 ['expanded_conv_depthwise[0][0]']
tchNormalization) )
expanded_conv_depthwise_relu ( (None, 112, 112, 16 0 ['expanded_conv_depthwise_BN[0][0
ReLU) ) ]']
expanded_conv_project (Conv2D) (None, 112, 112, 8) 128 ['expanded_conv_depthwise_relu[0]
[0]']
expanded_conv_project_BN (Batc (None, 112, 112, 8) 32 ['expanded_conv_project[0][0]']
hNormalization)
block_1_expand (Conv2D) (None, 112, 112, 48 384 ['expanded_conv_project_BN[0][0]'
) ]
block_1_expand_BN (BatchNormal (None, 112, 112, 48 192 ['block_1_expand[0][0]']
ization) )
block_1_expand_relu (ReLU) (None, 112, 112, 48 0 ['block_1_expand_BN[0][0]']
)
block_1_pad (ZeroPadding2D) (None, 113, 113, 48 0 ['block_1_expand_relu[0][0]']
)
block_1_depthwise (DepthwiseCo (None, 56, 56, 48) 432 ['block_1_pad[0][0]']
nv2D)
block_1_depthwise_BN (BatchNor (None, 56, 56, 48) 192 ['block_1_depthwise[0][0]']
malization)
block_1_depthwise_relu (ReLU) (None, 56, 56, 48) 0 ['block_1_depthwise_BN[0][0]']
block_1_project (Conv2D) (None, 56, 56, 8) 384 ['block_1_depthwise_relu[0][0]']
block_1_project_BN (BatchNorma (None, 56, 56, 8) 32 ['block_1_project[0][0]']
lization)
block_2_expand (Conv2D) (None, 56, 56, 48) 384 ['block_1_project_BN[0][0]']
block_2_expand_BN (BatchNormal (None, 56, 56, 48) 192 ['block_2_expand[0][0]']
ization)
block_2_expand_relu (ReLU) (None, 56, 56, 48) 0 ['block_2_expand_BN[0][0]']
block_2_depthwise (DepthwiseCo (None, 56, 56, 48) 432 ['block_2_expand_relu[0][0]']
nv2D)
block_2_depthwise_BN (BatchNor (None, 56, 56, 48) 192 ['block_2_depthwise[0][0]']
malization)
block_2_depthwise_relu (ReLU) (None, 56, 56, 48) 0 ['block_2_depthwise_BN[0][0]']
block_2_project (Conv2D) (None, 56, 56, 8) 384 ['block_2_depthwise_relu[0][0]']
block_2_project_BN (BatchNorma (None, 56, 56, 8) 32 ['block_2_project[0][0]']
lization)
block_2_add (Add) (None, 56, 56, 8) 0 ['block_1_project_BN[0][0]',
'block_2_project_BN[0][0]']
block_3_expand (Conv2D) (None, 56, 56, 48) 384 ['block_2_add[0][0]']
block_3_expand_BN (BatchNormal (None, 56, 56, 48) 192 ['block_3_expand[0][0]']
ization)
block_3_expand_relu (ReLU) (None, 56, 56, 48) 0 ['block_3_expand_BN[0][0]']
block_3_pad (ZeroPadding2D) (None, 57, 57, 48) 0 ['block_3_expand_relu[0][0]']
block_3_depthwise (DepthwiseCo (None, 28, 28, 48) 432 ['block_3_pad[0][0]']
nv2D)
block_3_depthwise_BN (BatchNor (None, 28, 28, 48) 192 ['block_3_depthwise[0][0]']
malization)
block_3_depthwise_relu (ReLU) (None, 28, 28, 48) 0 ['block_3_depthwise_BN[0][0]']
block_3_project (Conv2D) (None, 28, 28, 16) 768 ['block_3_depthwise_relu[0][0]']
block_3_project_BN (BatchNorma (None, 28, 28, 16) 64 ['block_3_project[0][0]']
lization)
block_4_expand (Conv2D) (None, 28, 28, 96) 1536 ['block_3_project_BN[0][0]']
block_4_expand_BN (BatchNormal (None, 28, 28, 96) 384 ['block_4_expand[0][0]']
ization)
block_4_expand_relu (ReLU) (None, 28, 28, 96) 0 ['block_4_expand_BN[0][0]']
block_4_depthwise (DepthwiseCo (None, 28, 28, 96) 864 ['block_4_expand_relu[0][0]']
nv2D)
block_4_depthwise_BN (BatchNor (None, 28, 28, 96) 384 ['block_4_depthwise[0][0]']
malization)
block_4_depthwise_relu (ReLU) (None, 28, 28, 96) 0 ['block_4_depthwise_BN[0][0]']
block_4_project (Conv2D) (None, 28, 28, 16) 1536 ['block_4_depthwise_relu[0][0]']
block_4_project_BN (BatchNorma (None, 28, 28, 16) 64 ['block_4_project[0][0]']
lization)
block_4_add (Add) (None, 28, 28, 16) 0 ['block_3_project_BN[0][0]',
'block_4_project_BN[0][0]']
block_5_expand (Conv2D) (None, 28, 28, 96) 1536 ['block_4_add[0][0]']
block_5_expand_BN (BatchNormal (None, 28, 28, 96) 384 ['block_5_expand[0][0]']
ization)
block_5_expand_relu (ReLU) (None, 28, 28, 96) 0 ['block_5_expand_BN[0][0]']
block_5_depthwise (DepthwiseCo (None, 28, 28, 96) 864 ['block_5_expand_relu[0][0]']
nv2D)
block_5_depthwise_BN (BatchNor (None, 28, 28, 96) 384 ['block_5_depthwise[0][0]']
malization)
block_5_depthwise_relu (ReLU) (None, 28, 28, 96) 0 ['block_5_depthwise_BN[0][0]']
block_5_project (Conv2D) (None, 28, 28, 16) 1536 ['block_5_depthwise_relu[0][0]']
block_5_project_BN (BatchNorma (None, 28, 28, 16) 64 ['block_5_project[0][0]']
lization)
block_5_add (Add) (None, 28, 28, 16) 0 ['block_4_add[0][0]',
'block_5_project_BN[0][0]']
block_6_expand (Conv2D) (None, 28, 28, 96) 1536 ['block_5_add[0][0]']
block_6_expand_BN (BatchNormal (None, 28, 28, 96) 384 ['block_6_expand[0][0]']
ization)
block_6_expand_relu (ReLU) (None, 28, 28, 96) 0 ['block_6_expand_BN[0][0]']
block_6_pad (ZeroPadding2D) (None, 29, 29, 96) 0 ['block_6_expand_relu[0][0]']
block_6_depthwise (DepthwiseCo (None, 14, 14, 96) 864 ['block_6_pad[0][0]']
nv2D)
block_6_depthwise_BN (BatchNor (None, 14, 14, 96) 384 ['block_6_depthwise[0][0]']
malization)
block_6_depthwise_relu (ReLU) (None, 14, 14, 96) 0 ['block_6_depthwise_BN[0][0]']
block_6_project (Conv2D) (None, 14, 14, 24) 2304 ['block_6_depthwise_relu[0][0]']
block_6_project_BN (BatchNorma (None, 14, 14, 24) 96 ['block_6_project[0][0]']
lization)
block_7_expand (Conv2D) (None, 14, 14, 144) 3456 ['block_6_project_BN[0][0]']
block_7_expand_BN (BatchNormal (None, 14, 14, 144) 576 ['block_7_expand[0][0]']
ization)
block_7_expand_relu (ReLU) (None, 14, 14, 144) 0 ['block_7_expand_BN[0][0]']
block_7_depthwise (DepthwiseCo (None, 14, 14, 144) 1296 ['block_7_expand_relu[0][0]']
nv2D)
block_7_depthwise_BN (BatchNor (None, 14, 14, 144) 576 ['block_7_depthwise[0][0]']
malization)
block_7_depthwise_relu (ReLU) (None, 14, 14, 144) 0 ['block_7_depthwise_BN[0][0]']
block_7_project (Conv2D) (None, 14, 14, 24) 3456 ['block_7_depthwise_relu[0][0]']
block_7_project_BN (BatchNorma (None, 14, 14, 24) 96 ['block_7_project[0][0]']
lization)
block_7_add (Add) (None, 14, 14, 24) 0 ['block_6_project_BN[0][0]',
'block_7_project_BN[0][0]']
block_8_expand (Conv2D) (None, 14, 14, 144) 3456 ['block_7_add[0][0]']
block_8_expand_BN (BatchNormal (None, 14, 14, 144) 576 ['block_8_expand[0][0]']
ization)
block_8_expand_relu (ReLU) (None, 14, 14, 144) 0 ['block_8_expand_BN[0][0]']
block_8_depthwise (DepthwiseCo (None, 14, 14, 144) 1296 ['block_8_expand_relu[0][0]']
nv2D)
block_8_depthwise_BN (BatchNor (None, 14, 14, 144) 576 ['block_8_depthwise[0][0]']
malization)
block_8_depthwise_relu (ReLU) (None, 14, 14, 144) 0 ['block_8_depthwise_BN[0][0]']
block_8_project (Conv2D) (None, 14, 14, 24) 3456 ['block_8_depthwise_relu[0][0]']
block_8_project_BN (BatchNorma (None, 14, 14, 24) 96 ['block_8_project[0][0]']
lization)
block_8_add (Add) (None, 14, 14, 24) 0 ['block_7_add[0][0]',
'block_8_project_BN[0][0]']
block_9_expand (Conv2D) (None, 14, 14, 144) 3456 ['block_8_add[0][0]']
block_9_expand_BN (BatchNormal (None, 14, 14, 144) 576 ['block_9_expand[0][0]']
ization)
block_9_expand_relu (ReLU) (None, 14, 14, 144) 0 ['block_9_expand_BN[0][0]']
block_9_depthwise (DepthwiseCo (None, 14, 14, 144) 1296 ['block_9_expand_relu[0][0]']
nv2D)
block_9_depthwise_BN (BatchNor (None, 14, 14, 144) 576 ['block_9_depthwise[0][0]']
malization)
block_9_depthwise_relu (ReLU) (None, 14, 14, 144) 0 ['block_9_depthwise_BN[0][0]']
block_9_project (Conv2D) (None, 14, 14, 24) 3456 ['block_9_depthwise_relu[0][0]']
block_9_project_BN (BatchNorma (None, 14, 14, 24) 96 ['block_9_project[0][0]']
lization)
block_9_add (Add) (None, 14, 14, 24) 0 ['block_8_add[0][0]',
'block_9_project_BN[0][0]']
block_10_expand (Conv2D) (None, 14, 14, 144) 3456 ['block_9_add[0][0]']
block_10_expand_BN (BatchNorma (None, 14, 14, 144) 576 ['block_10_expand[0][0]']
lization)
block_10_expand_relu (ReLU) (None, 14, 14, 144) 0 ['block_10_expand_BN[0][0]']
block_10_depthwise (DepthwiseC (None, 14, 14, 144) 1296 ['block_10_expand_relu[0][0]']
onv2D)
block_10_depthwise_BN (BatchNo (None, 14, 14, 144) 576 ['block_10_depthwise[0][0]']
rmalization)
block_10_depthwise_relu (ReLU) (None, 14, 14, 144) 0 ['block_10_depthwise_BN[0][0]']
block_10_project (Conv2D) (None, 14, 14, 32) 4608 ['block_10_depthwise_relu[0][0]']
block_10_project_BN (BatchNorm (None, 14, 14, 32) 128 ['block_10_project[0][0]']
alization)
block_11_expand (Conv2D) (None, 14, 14, 192) 6144 ['block_10_project_BN[0][0]']
block_11_expand_BN (BatchNorma (None, 14, 14, 192) 768 ['block_11_expand[0][0]']
lization)
block_11_expand_relu (ReLU) (None, 14, 14, 192) 0 ['block_11_expand_BN[0][0]']
block_11_depthwise (DepthwiseC (None, 14, 14, 192) 1728 ['block_11_expand_relu[0][0]']
onv2D)
block_11_depthwise_BN (BatchNo (None, 14, 14, 192) 768 ['block_11_depthwise[0][0]']
rmalization)
block_11_depthwise_relu (ReLU) (None, 14, 14, 192) 0 ['block_11_depthwise_BN[0][0]']
block_11_project (Conv2D) (None, 14, 14, 32) 6144 ['block_11_depthwise_relu[0][0]']
block_11_project_BN (BatchNorm (None, 14, 14, 32) 128 ['block_11_project[0][0]']
alization)
block_11_add (Add) (None, 14, 14, 32) 0 ['block_10_project_BN[0][0]',
'block_11_project_BN[0][0]']
block_12_expand (Conv2D) (None, 14, 14, 192) 6144 ['block_11_add[0][0]']
block_12_expand_BN (BatchNorma (None, 14, 14, 192) 768 ['block_12_expand[0][0]']
lization)
block_12_expand_relu (ReLU) (None, 14, 14, 192) 0 ['block_12_expand_BN[0][0]']
block_12_depthwise (DepthwiseC (None, 14, 14, 192) 1728 ['block_12_expand_relu[0][0]']
onv2D)
block_12_depthwise_BN (BatchNo (None, 14, 14, 192) 768 ['block_12_depthwise[0][0]']
rmalization)
block_12_depthwise_relu (ReLU) (None, 14, 14, 192) 0 ['block_12_depthwise_BN[0][0]']
block_12_project (Conv2D) (None, 14, 14, 32) 6144 ['block_12_depthwise_relu[0][0]']
block_12_project_BN (BatchNorm (None, 14, 14, 32) 128 ['block_12_project[0][0]']
alization)
block_12_add (Add) (None, 14, 14, 32) 0 ['block_11_add[0][0]',
'block_12_project_BN[0][0]']
block_13_expand (Conv2D) (None, 14, 14, 192) 6144 ['block_12_add[0][0]']
block_13_expand_BN (BatchNorma (None, 14, 14, 192) 768 ['block_13_expand[0][0]']
lization)
block_13_expand_relu (ReLU) (None, 14, 14, 192) 0 ['block_13_expand_BN[0][0]']
up_sampling2d (UpSampling2D) (None, 28, 28, 192) 0 ['block_13_expand_relu[0][0]']
concatenate (Concatenate) (None, 28, 28, 288) 0 ['up_sampling2d[0][0]',
'block_6_expand_relu[0][0]']
conv2d (Conv2D) (None, 28, 28, 64) 165952 ['concatenate[0][0]']
batch_normalization (BatchNorm (None, 28, 28, 64) 256 ['conv2d[0][0]']
alization)
activation (Activation) (None, 28, 28, 64) 0 ['batch_normalization[0][0]']
conv2d_1 (Conv2D) (None, 28, 28, 64) 36928 ['activation[0][0]']
batch_normalization_1 (BatchNo (None, 28, 28, 64) 256 ['conv2d_1[0][0]']
rmalization)
activation_1 (Activation) (None, 28, 28, 64) 0 ['batch_normalization_1[0][0]']
up_sampling2d_1 (UpSampling2D) (None, 56, 56, 64) 0 ['activation_1[0][0]']
concatenate_1 (Concatenate) (None, 56, 56, 112) 0 ['up_sampling2d_1[0][0]',
'block_3_expand_relu[0][0]']
conv2d_2 (Conv2D) (None, 56, 56, 48) 48432 ['concatenate_1[0][0]']
batch_normalization_2 (BatchNo (None, 56, 56, 48) 192 ['conv2d_2[0][0]']
rmalization)
activation_2 (Activation) (None, 56, 56, 48) 0 ['batch_normalization_2[0][0]']
conv2d_3 (Conv2D) (None, 56, 56, 48) 20784 ['activation_2[0][0]']
batch_normalization_3 (BatchNo (None, 56, 56, 48) 192 ['conv2d_3[0][0]']
rmalization)
activation_3 (Activation) (None, 56, 56, 48) 0 ['batch_normalization_3[0][0]']
up_sampling2d_2 (UpSampling2D) (None, 112, 112, 48 0 ['activation_3[0][0]']
)
concatenate_2 (Concatenate) (None, 112, 112, 96 0 ['up_sampling2d_2[0][0]',
) 'block_1_expand_relu[0][0]']
conv2d_4 (Conv2D) (None, 112, 112, 32 27680 ['concatenate_2[0][0]']
)
batch_normalization_4 (BatchNo (None, 112, 112, 32 128 ['conv2d_4[0][0]']
rmalization) )
activation_4 (Activation) (None, 112, 112, 32 0 ['batch_normalization_4[0][0]']
)
conv2d_5 (Conv2D) (None, 112, 112, 32 9248 ['activation_4[0][0]']
)
batch_normalization_5 (BatchNo (None, 112, 112, 32 128 ['conv2d_5[0][0]']
rmalization) )
activation_5 (Activation) (None, 112, 112, 32 0 ['batch_normalization_5[0][0]']
)
up_sampling2d_3 (UpSampling2D) (None, 224, 224, 32 0 ['activation_5[0][0]']
)
concatenate_3 (Concatenate) (None, 224, 224, 35 0 ['up_sampling2d_3[0][0]',
) 'input_image[0][0]']
conv2d_6 (Conv2D) (None, 224, 224, 16 5056 ['concatenate_3[0][0]']
)
batch_normalization_6 (BatchNo (None, 224, 224, 16 64 ['conv2d_6[0][0]']
rmalization) )
activation_6 (Activation) (None, 224, 224, 16 0 ['batch_normalization_6[0][0]']
)
conv2d_7 (Conv2D) (None, 224, 224, 16 2320 ['activation_6[0][0]']
)
batch_normalization_7 (BatchNo (None, 224, 224, 16 64 ['conv2d_7[0][0]']
rmalization) )
activation_7 (Activation) (None, 224, 224, 16 0 ['batch_normalization_7[0][0]']
)
conv2d_8 (Conv2D) (None, 224, 224, 1) 17 ['activation_7[0][0]']
activation_8 (Activation) (None, 224, 224, 1) 0 ['conv2d_8[0][0]']
==================================================================================================
Total params: 416,209
Trainable params: 409,025
Non-trainable params: 7,184
__________________________________________________________________________________________________
test_steps = (len(X_test)//BATCH)
if len(X_test) % BATCH != 0:
test_steps += 1
new_model.evaluate(X_test, y_test, steps=test_steps)
3/3 [==============================] - 5s 303ms/step - loss: 0.4600 - dice_coef: 0.5342 - recall: 0.7583 - precision: 0.8071
[0.46004775166511536, 0.5342147946357727, 0.7583245038986206, 0.807067334651947]
Predicting an image that was not used for training and testing the model
filename ='Data/Part+1Test+Data+-+Prediction+Image.jpeg'
unscaled = cv2.imread(filename)
image = cv2.resize(unscaled, (IMAGE_WIDTH, IMAGE_HEIGHT))
feat_scaled = preprocess_input(np.array(image, dtype=np.float32))
feat_scaled
array([[[ 0.5686275 , 0.4666667 , 0.45098042],
[ 0.56078434, 0.45882356, 0.4431373 ],
[ 0.5764706 , 0.47450984, 0.45882356],
...,
[-0.17647058, -0.5058824 , -0.73333335],
[-0.27058822, -0.49019605, -0.75686276],
[-0.27843136, -0.47450978, -0.7411765 ]],
[[ 0.6 , 0.49803925, 0.48235297],
[ 0.6 , 0.4901961 , 0.48235297],
[ 0.6313726 , 0.5294118 , 0.5137255 ],
...,
[-0.20784312, -0.52156866, -0.7490196 ],
[-0.16862744, -0.40392154, -0.6627451 ],
[-0.19215685, -0.41176468, -0.6784314 ]],
[[ 0.54509807, 0.45098042, 0.41960788],
[ 0.47450984, 0.3803922 , 0.34901965],
[ 0.5372549 , 0.4431373 , 0.4039216 ],
...,
[-0.14509803, -0.42745095, -0.6627451 ],
[-0.3098039 , -0.5686275 , -0.827451 ],
[-0.34117645, -0.5921569 , -0.8509804 ]],
...,
[[-1. , -1. , -1. ],
[-1. , -1. , -1. ],
[-1. , -1. , -1. ],
...,
[-1. , -1. , -1. ],
[-1. , -1. , -1. ],
[-1. , -1. , -1. ]],
[[-1. , -1. , -1. ],
[-1. , -1. , -1. ],
[-1. , -1. , -1. ],
...,
[-1. , -1. , -1. ],
[-1. , -1. , -1. ],
[-1. , -1. , -1. ]],
[[-1. , -1. , -1. ],
[-1. , -1. , -1. ],
[-1. , -1. , -1. ],
...,
[-1. , -1. , -1. ],
[-1. , -1. , -1. ],
[-1. , -1. , -1. ]]], dtype=float32)
y_pred = new_model.predict(np.array([feat_scaled]))
y_pred
array([[[[0.17036197],
[0.15527216],
[0.10860014],
...,
[0.10471413],
[0.11665457],
[0.14584196]],
[[0.1503942 ],
[0.14134693],
[0.10738048],
...,
[0.10415772],
[0.12254664],
[0.1267032 ]],
[[0.10826859],
[0.10619867],
[0.10784283],
...,
[0.10895398],
[0.10891101],
[0.11973789]],
...,
[[0.10260427],
[0.10458368],
[0.11108592],
...,
[0.10997057],
[0.10522434],
[0.11393836]],
[[0.10231951],
[0.10114568],
[0.10504085],
...,
[0.10601571],
[0.11285844],
[0.12970859]],
[[0.13998067],
[0.12768617],
[0.12298313],
...,
[0.12232521],
[0.11775053],
[0.15591204]]]], dtype=float32)
pred_mask = cv2.resize((1.0*(y_pred[0] > .5)), (IMAGE_WIDTH,IMAGE_HEIGHT))
plt.imshow(feat_scaled)
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
<matplotlib.image.AxesImage at 0x17780f35610>
plt.imshow(pred_mask)
<matplotlib.image.AxesImage at 0x17780f8dca0>
The model was able to detect two faces in the image correctly.
from tqdm.notebook import trange, tqdm
from IPython.display import Image, display, Markdown, clear_output
from zipfile import ZipFile as z
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
from mpl_toolkits.axes_grid1 import ImageGrid
import glob
import os
Project_path = 'C:/Users/AI_SG/Documents/Upendran/Course/python/Projects/Computer Vision/PROJECT 2/'
from zipfile import ZipFile
# specifying the zip file name
file_name = Project_path +"Data/Aligned Face Dataset from Pinterest - CV project 2.zip"
# opening the zip file in READ mode
with ZipFile(file_name, 'r') as z:
# extracting all the files
print('Extracting all the files now...')
z.extractall()
print('Done!')
Extracting all the files now...
--------------------------------------------------------------------------- FileNotFoundError Traceback (most recent call last) ~\AppData\Local\Temp/ipykernel_3164/3042312314.py in <module> 8 # extracting all the files 9 print('Extracting all the files now...') ---> 10 z.extractall() 11 print('Done!') ~\anaconda3\lib\zipfile.py in extractall(self, path, members, pwd) 1645 1646 for zipinfo in members: -> 1647 self._extract_member(zipinfo, path, pwd) 1648 1649 @classmethod ~\anaconda3\lib\zipfile.py in _extract_member(self, member, targetpath, pwd) 1699 1700 with self.open(member, pwd=pwd) as source, \ -> 1701 open(targetpath, "wb") as target: 1702 shutil.copyfileobj(source, target) 1703 FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\AI_SG\\Documents\\Upendran\\Course\\python\\Projects\\Computer Vision\\PROJECT 2\\PINS\\pins_Natalie Portman \\Natalie Portman 0.jpg'
TRAIN_FOLDER = 'PINS'
from glob import glob
images = []
classes=[]
missing=0
for class_folder_name in os.listdir(TRAIN_FOLDER):
class_folder_path = os.path.join(TRAIN_FOLDER, class_folder_name)
class_label = class_folder_name
for image_path in glob(os.path.join(class_folder_path, "*.jpg")):
image_bgr = cv2.imread(image_path, cv2.IMREAD_COLOR)
if image_bgr is None: # if the file contain any missing value ignore it
missing += 1
continue
images.append(image_bgr)
classes.append(class_label)
# resize image
def resize_images(img):
img = np.array(img).astype(np.uint8)
#print(img.dtype)
res = cv2.resize(img,(224,224), interpolation = cv2.INTER_CUBIC)
return res
#save resized images into images.
images = [resize_images(img) for img in images]
#see number of images in each label
images = np.array(images)
classes = np.array(classes)
print("images shape: ", images.shape)
print("classes shape: ", classes.shape)
images shape: (4382, 224, 224, 3) classes shape: (4382,)
import numpy as np
import os
class IdentityMetadata():
def __init__(self, base, name, file):
# print(base, name, file)
# dataset base directory
self.base = base
# identity name
self.name = name
# image file name
self.file = file
def __repr__(self):
return self.image_path()
def image_path(self):
return os.path.join(self.base, self.name, self.file)
def load_metadata(path):
metadata = []
for i in os.listdir(path):
for f in os.listdir(os.path.join(path, i)):
# Check file extension. Allow only jpg/jpeg' files.
ext = os.path.splitext(f)[1]
if ext == '.jpg' or ext == '.jpeg':
metadata.append(IdentityMetadata(path, i, f))
return np.array(metadata)
# metadata = load_metadata('images')
metadata = load_metadata('PINS')
import cv2
def load_image(path):
img = cv2.imread(path, 1)
# OpenCV loads images with color channels
# in BGR order. So we need to reverse them
return img[...,::-1]
# Load an image
# for example, loading the image with index 1
plt.imshow(load_image(metadata[0].image_path()))
<matplotlib.image.AxesImage at 0x17833e918e0>
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import ZeroPadding2D, Convolution2D, MaxPooling2D, Dropout, Flatten, Activation
def vgg_face():
model = Sequential()
model.add(ZeroPadding2D((1,1),input_shape=(224,224, 3)))
model.add(Convolution2D(64, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(128, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(128, (3, 3), activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, (3, 3), activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(Convolution2D(4096, (7, 7), activation='relu'))
model.add(Dropout(0.5))
model.add(Convolution2D(4096, (1, 1), activation='relu'))
model.add(Dropout(0.5))
model.add(Convolution2D(2622, (1, 1)))
model.add(Flatten())
model.add(Activation('softmax'))
return model
from tensorflow.keras.models import model_from_json
weights_file = 'Data/vgg_face_weights.h5'
model = vgg_face()
model.load_weights(weights_file)
from tensorflow.keras.models import Model
vgg_face_descriptor = Model(inputs=model.layers[0].input, outputs=model.layers[-2].output)
# Get embedding vector for first image in the metadata using the pre-trained model
img_path = metadata[0].image_path()
img = load_image(img_path)
# Normalising pixel values from [0-255] to [0-1]: scale RGB values to interval [0,1]
img = (img / 255.).astype(np.float32)
img = cv2.resize(img, dsize = (224,224))
print(img.shape)
# Obtain embedding vector for an image
# Get the embedding vector for the above image using vgg_face_descriptor model and print the shape
embedding_vector = vgg_face_descriptor.predict(np.expand_dims(img, axis=0))[0]
print(embedding_vector.shape)
(224, 224, 3) (2622,)
Write code to iterate through metadata and create embeddings for each image using vgg_face_descriptor.predict() and store in a list with name embeddings
If there is any error in reading any image in the dataset, fill the emebdding vector of that image with 2622-zeroes as the final embedding from the model is of length 2622.
embeddings = np.zeros((metadata.shape[0], 2622))
for i, m in enumerate(metadata):
img_path = metadata[i].image_path()
img = load_image(img_path)
img = (img / 255.).astype(np.float32)
img = cv2.resize(img, dsize = (224,224))
embedding_vector = vgg_face_descriptor.predict(np.expand_dims(img, axis=0))[0]
embeddings[i]=embedding_vector
len(embeddings)
4382
embeddings
array([[ 0.03170304, -0.0150513 , -0.01243402, ..., 0.00043139,
0.00219081, -0.00908097],
[ 0.03497701, -0.00105061, -0.01248934, ..., -0.0105309 ,
0.00179322, 0.02439155],
[ 0.02801891, -0.00112631, -0.0126575 , ..., -0.00904751,
-0.00578403, 0.0212583 ],
...,
[ 0.00633465, 0.00460462, -0.00857739, ..., -0.00243808,
0.00556754, 0.00227435],
[ 0.01565452, 0.00558202, -0.00874991, ..., -0.00294676,
0.00372011, -0.00933463],
[ 0.03565785, 0.01043394, -0.00349362, ..., -0.0022366 ,
0.00233227, 0.0023782 ]])
# At first, we have to import the 'Shelve' module.
import shelve
# In this step, we create a shelf file.
shfile = shelve.open("shelf_file")
# we create a data object which in this case is a book_list.
embed_list = embeddings
# we are assigning a dictionary key to the list
# which we will want to retrieve
shfile['embed_list']= embed_list
# now, we simply close the shelf file.
shfile.close()
# At first, we import the 'Shelve' module.
import shelve
# In this step, we create a shelf file.
var = shelve.open("shelf_file")
# Now, this 'var' variable points to all the
# data objects in the file 'shelf_file'.
# print(var['embed_list'])
new_embed = var['embed_list']
# now, we simply close the file 'shelf_file'.
var.close()
len(new_embed)
4382
new_embed
array([[ 0.03170304, -0.0150513 , -0.01243402, ..., 0.00043139,
0.00219081, -0.00908097],
[ 0.03497701, -0.00105061, -0.01248934, ..., -0.0105309 ,
0.00179322, 0.02439155],
[ 0.02801891, -0.00112631, -0.0126575 , ..., -0.00904751,
-0.00578403, 0.0212583 ],
...,
[ 0.00633465, 0.00460462, -0.00857739, ..., -0.00243808,
0.00556754, 0.00227435],
[ 0.01565452, 0.00558202, -0.00874991, ..., -0.00294676,
0.00372011, -0.00933463],
[ 0.03565785, 0.01043394, -0.00349362, ..., -0.0022366 ,
0.00233227, 0.0023782 ]])
def distance(emb1, emb2):
return np.sum(np.square(emb1 - emb2))
import matplotlib.pyplot as plt
def show_pair(idx1, idx2):
plt.figure(figsize=(8,3))
plt.suptitle(f'Distance = {distance(embeddings[idx1], embeddings[idx2]):.2f}')
plt.subplot(121)
plt.imshow(load_image(metadata[idx1].image_path()))
plt.subplot(122)
plt.imshow(load_image(metadata[idx2].image_path()));
show_pair(2, 3)
show_pair(2, 180)
show_pair(30, 31)
show_pair(30, 100)
show_pair(70, 72)
show_pair(70, 115)
train_idx = np.arange(metadata.shape[0]) % 9 != 0
test_idx = np.arange(metadata.shape[0]) % 9 == 0
# one half as train examples of 10 identities
X_train = embeddings[train_idx]
# another half as test examples of 10 identities
X_test = embeddings[test_idx]
targets = np.array([m.name for m in metadata])
y_train = targets[train_idx]
y_test = targets[test_idx]
from sklearn.preprocessing import LabelEncoder
encoder = LabelEncoder()
# Numerical encoding of identities
y_train = encoder.fit_transform(y_train)
y_test = encoder.transform(y_test)
# Standarize features
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
from sklearn.decomposition import PCA
pca = PCA(n_components=128, svd_solver='randomized', whiten=True)
X_train = pca.fit_transform(X_train)
X_test = pca.transform(X_test)
X_train.shape,X_test.shape
((3895, 128), (487, 128))
y_train.shape,y_test.shape
((7790,), (974,))
from sklearn.svm import SVC
from sklearn.model_selection import learning_curve, GridSearchCV
param_grid = [
{'C': [1, 10, 100, 1000], 'kernel': ['linear']},
{'C': [1, 10, 100, 1000], 'gamma': [0.001, 0.0001], 'kernel': ['rbf']},
]
svc = SVC()
clf = GridSearchCV(svc, param_grid, verbose=10, n_jobs=-1)
clf.fit(X_train, y_train)
Fitting 5 folds for each of 12 candidates, totalling 60 fits
GridSearchCV(estimator=SVC(), n_jobs=-1,
param_grid=[{'C': [1, 10, 100, 1000], 'kernel': ['linear']},
{'C': [1, 10, 100, 1000], 'gamma': [0.001, 0.0001],
'kernel': ['rbf']}],
verbose=10)
def getAccuracy(testSet, predictions):
correct = 0
for x in range(len(testSet)):
if testSet[x]== predictions[x]:
correct += 1
return (correct/float(len(testSet))) * 100.0
print(clf.score(X_train, y_train))
print(clf.score(X_test, y_test))
0.989987163029525 0.9856262833675564
y_pred = clf.predict(X_test)
svm_acc=getAccuracy(y_test , y_pred)
svm_acc
98.56262833675564
import warnings
# Suppress LabelEncoder warning
warnings.filterwarnings('ignore')
example_idx = 10
example_image = load_image(metadata[test_idx][example_idx].image_path())
example_prediction = clf.predict([X_test[example_idx]])
example_identity = encoder.inverse_transform(example_prediction)[0]
plt.imshow(example_image)
plt.title(f'Identified as {example_identity}');
model_performance = pd.DataFrame(columns=['Model', 'Accuracy','Label', 'Predicted Person'])
Prediction of Test Image 1
# Get embedding vector for first image in the metadata using the pre-trained model
img_path = 'Data/Benedict+Cumberbatch9.jpg'
img = load_image(img_path)
# Normalising pixel values from [0-255] to [0-1]: scale RGB values to interval [0,1]
img = (img / 255.).astype(np.float32)
img = cv2.resize(img, dsize = (224,224))
# print(img.shape)
# Obtain embedding vector for an image
# Get the embedding vector for the above image using vgg_face_descriptor model and print the shape
prd_embedding_vector1 = vgg_face_descriptor.predict(np.expand_dims(img, axis=0))[0]
# print(prd_embedding_vector1.shape)
# print(prd_embedding_vector1)
X_pred = prd_embedding_vector1
X_pred = X_pred.reshape(1,-1)
X_pred = sc.transform(X_pred)
X_pred = pca.transform(X_pred)
pred_svm = clf.predict(X_pred)
print(f"The predicted label is:'{pred_svm}'")
The predicted label is:'[5]'
example_identity = enc.inverse_transform(np.ravel(pred_svm))[0]
plt.imshow(img)
plt.title(f'Recognized as {example_identity}');
model_performance = model_performance.append({'Model':'SVM',
'Accuracy': svm_acc,
'Label': pred_svm,
'Predicted Person': example_identity
}, ignore_index=True)
model_performance
| Model | Accuracy | Label | Predicted Person | |
|---|---|---|---|---|
| 0 | SVM | 98.562628 | [5] | BENEDICT_CUMBERBATCH |
Prediction of Test Image 2
# Get embedding vector for first image in the metadata using the pre-trained model
img_path = 'Data/Dwayne+Johnson4.jpg'
img = load_image(img_path)
# Normalising pixel values from [0-255] to [0-1]: scale RGB values to interval [0,1]
img = (img / 255.).astype(np.float32)
img = cv2.resize(img, dsize = (224,224))
# print(img.shape)
# Obtain embedding vector for an image
# Get the embedding vector for the above image using vgg_face_descriptor model and print the shape
prd_embedding_vector1 = vgg_face_descriptor.predict(np.expand_dims(img, axis=0))[0]
# print(prd_embedding_vector1.shape)
# print(prd_embedding_vector1)
X_pred = prd_embedding_vector1
X_pred = X_pred.reshape(1,-1)
#StandardScaler
X_pred = sc.transform(X_pred)
#Passing through PCA
X_pred = pca.transform(X_pred)
# Predict using SVM model
pred_svm = clf.predict(X_pred)
print(f"The predicted label is:'{pred_svm}'")
#show the predicted label and image
example_identity = enc.inverse_transform(np.ravel(pred_svm))[0]
plt.imshow(img)
plt.title(f'Recognized as {example_identity}');
The predicted label is:'[19]'
model_performance = model_performance.append({'Model':'SVM',
'Accuracy': svm_acc,
'Label': pred_svm,
'Predicted Person': example_identity
}, ignore_index=True)
model_performance
| Model | Accuracy | Label | Predicted Person | |
|---|---|---|---|---|
| 0 | SVM | 98.562628 | [5] | BENEDICT_CUMBERBATCH |
| 1 | SVM | 98.562628 | [19] | DWAYNE_JOHNSON |